package scripts;
import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class CatherbyFishAndCook extends ScriptMethods implements Runnable {

	private int rawID1;
	private int rawID2;
	private int cookedID1;
	private int cookedID2;
	private int burnedID1;
	private int burnedID2;
	private int spotID;
	private int spotClick;

	public void start() {
		
		getAccountSettings();
		System.gc();
		
		Object[] options = {"Net", "Harpoon", "Lobster pot", "Harpoon (Sharks)"};
		
		String res1 = (String)JOptionPane.showInputDialog(null, "", "CFAC", JOptionPane.PLAIN_MESSAGE, null, options, options[2]);
		
		if (res1.equals("Net")) {
			spotID = 193;
			spotClick = 1;
			rawID1 = 349;
			rawID2 = 351;
			cookedID1 = 350;
			cookedID2 = 352;
			burnedID1 = 353;
			burnedID2 = 777;
		}

		if (res1.equals("Harpoon")) {
			spotID = 194;
			spotClick = 1;
			rawID1 = 366;
			cookedID1 = 367;
			rawID2 = 369;
			cookedID2 = 370;
			burnedID1 = 368;
			burnedID2 = 371;
		}

		if (res1.equals("Lobster pot")) {
			spotID = 194;
			spotClick = 2;
			rawID1 = 372;
			cookedID1 = 373;
			rawID2 = 777;
			cookedID2 = 777;
			burnedID1 = 374;
			burnedID2 = 777;
		}

		if (res1.equals("Harpoon (Sharks)")) {
			spotID = 261;
			spotClick = 2;
			rawID1 = 545;
			cookedID1 = 546;
			rawID2 = 777;
			cookedID2 = 777;
			burnedID1 = 547;
			burnedID2 = 777;
		}

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptThread = null;
		scriptRunning = false;
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();

				if (bankOpen() && !isSleeping()) {
					if (invContains(cookedID1)) {
						depositAll(cookedID1);
						wait(random(600, 750));
					}
					if (invContains(burnedID1)) {
						depositAll(burnedID1);
						wait(random(600, 750));
					}
					if (invContains(cookedID2)) {
						depositAll(cookedID2);
						wait(random(600, 750));
					}
					if (invContains(burnedID2)) {
						depositAll(burnedID2);
						wait(random(600, 750));
					}
					closeBank();
					wait(600);
				}

				if (getInventorySize() == 30 && !isSleeping()) { // Cook
					int[] range = getObjectById(11);
					if (range[0] != -1) {
						while (invContains(rawID1) && !isSleeping() && scriptRunning) {
							handleEvents();
							useItemOnObject(range[1], range[2], rawID1);
							wait(random(800, 1000));
						}
						while (invContains(rawID2) && !isSleeping() && scriptRunning) {
							handleEvents();
							useItemOnObject(range[1], range[2], rawID2);
							wait(random(800, 1000));
						}
					} else {
						walkToWait(422, 490);
					}
				}

				if (getInventorySize() == 30 && !invContains(rawID1) && !invContains(rawID2) && !isSleeping()) { // Bank
					if (ourPlayerX() >= 437) {
						if (!bankOpen()) {
							int[] bankDoor = getObjectById(64);
							if (bankDoor[0] != -1 && !bankOpen()) {
								System.out.println("Bank door closed, opening.");
								atObject(bankDoor[1], bankDoor[2]);
								wait(random(1000, 1200));
							} else {
								openBank();
								wait(2000);
							}
						}
					} else {
						int[] bankDoor = getObjectById(64);
						if (bankDoor[0] != -1 && !bankOpen()) {
								System.out.println("Bank door closed, opening.");
								atObject(bankDoor[1], bankDoor[2]);
								wait(random(1000, 1200));
						} else {
							walkToWait(440, 495);
						}
					}
				}

				if (getInventorySize() < 30 && !isSleeping()) { // Fish
					int[] fishies = getObjectById(spotID);
					if (fishies[0] != -1) {
						if (spotClick == 1) {
							atObject(fishies[1], fishies[2]);
							wait(random(650, 900));
						} else if (spotClick == 2) {
							atObject2(fishies[1], fishies[2]);
							wait(random(650, 900));
						}
					} else {
						walkToWait(410, 500);
					}
				}

				wait(random(50, 100));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (!isLoggedIn())
			doLogin();
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (getFatigue() > 79 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
	}
}